//
//  ZDPBModalAnimation.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/11/11.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDPBModalAnimation.h"
#import "ZDPhotoBrowserViewController.h"

@implementation ZDPBModalAnimation


- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.5;
}


- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    //获取modal视图
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    //获取容器视图
    UIView *transitionView = [transitionContext containerView];
    
    //添加到容器视图中
    [transitionView addSubview:toView];
    
    //设置toView的透明度
    toView.alpha = 0;
    
    //获取modal控制器
    ZDPhotoBrowserViewController *pbVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    //获取过渡视图
    UIImageView *tempImageView = [pbVC modalTempImageView];
    
    //添加过渡视图到容器视图中
    [transitionView addSubview:tempImageView];
    
    pbVC.photoBrowserCollectionView.hidden = YES;
    
    [UIView animateWithDuration:[self transitionDuration:nil] animations:^{
        
        toView.alpha = 1.0;
        
        //修改过渡视图的frame
        tempImageView.frame = [pbVC modalTempViewEndFrame];
        
    } completion:^(BOOL finished) {
        
        //移除过渡视图
        [tempImageView removeFromSuperview];
        
        pbVC.photoBrowserCollectionView.hidden = NO;
        
        pbVC.view.hidden = NO;
        
        [transitionContext completeTransition:YES];
    }];
}


@end
